<?php
define("EW_PAGE_ID", "add", TRUE); // Page ID
define("EW_TABLE_NAME", 'tb_users', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "tb_usersinfo.php" ?>
<?php include "userfn50.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$tb_users->Export = @$_GET["export"]; // Get export parameter
$sExport = $tb_users->Export; // Get export parameter, used in header
$sExportFile = $tb_users->TableVar; // Get export file, used in header
?>
<?php

// Load key values from QueryString
$bCopy = TRUE;
if (@$_GET["ID"] != "") {
  $tb_users->ID->setQueryStringValue($_GET["ID"]);
} else {
  $bCopy = FALSE;
}

// Create form object
$objForm = new cFormObj();

// Process form if post back
if (@$_POST["a_add"] <> "") {
  $tb_users->CurrentAction = $_POST["a_add"]; // Get form action
  LoadFormValues(); // Load form values
} else { // Not post back
  if ($bCopy) {
    $tb_users->CurrentAction = "C"; // Copy Record
  } else {
    $tb_users->CurrentAction = "I"; // Display Blank Record
    LoadDefaultValues(); // Load default values
  }
}

// Perform action based on action code
switch ($tb_users->CurrentAction) {
  case "I": // Blank record, no action required
		break;
  case "C": // Copy an existing record
   if (!LoadRow()) { // Load record based on key
      $_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
      Page_Terminate($tb_users->getReturnUrl()); // Clean up and return
    }
		break;
  case "A": // ' Add new record
		$tb_users->SendEmail = TRUE; // Send email on add success
    if (AddRow()) { // Add successful
      $_SESSION[EW_SESSION_MESSAGE] = "Add New Record Successful"; // Set up success message
      Page_Terminate($tb_users->KeyUrl($tb_users->getReturnUrl())); // Clean up and return
    } else {
      RestoreFormValues(); // Add failed, restore form values
    }
}

// Render row based on row type
$tb_users->RowType = EW_ROWTYPE_ADD;  // Render add type
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "add"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_UserName"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - User Name"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_UserPWD"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - User PWD"))
				return false;
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Add to TABLE: Users List<br><br><a href="<?php echo $tb_users->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") { // Mesasge in Session, display
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
  $_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
}
?>
<form name="ftb_usersadd" id="ftb_usersadd" action="tb_usersadd.php" method="post" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_add" id="a_add" value="A">
<table class="ewTable">
  <tr class="ewTableRow">
    <td class="ewTableHeader">User Name<span class='ewmsg'>&nbsp;*</span></td>
    <td<?php echo $tb_users->UserName->CellAttributes() ?>><span id="cb_x_UserName">
<input type="text" name="x_UserName" id="x_UserName"  size="30" maxlength="45" value="<?php echo $tb_users->UserName->EditValue ?>"<?php echo $tb_users->UserName->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">User PWD<span class='ewmsg'>&nbsp;*</span></td>
    <td<?php echo $tb_users->UserPWD->CellAttributes() ?>><span id="cb_x_UserPWD">
<input type="text" name="x_UserPWD" id="x_UserPWD"  size="30" maxlength="45" value="<?php echo $tb_users->UserPWD->EditValue ?>"<?php echo $tb_users->UserPWD->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableRow">
    <td class="ewTableHeader">Status<span class='ewmsg'>&nbsp;*</span></td>
    <td<?php echo $tb_users->Status->CellAttributes() ?>><span id="cb_x_Status">
<select id="x_Status" name="x_Status"<?php echo $tb_users->Status->EditAttributes() ?>>
<!--option value="">Please Select</option-->
<?php
if (is_array($tb_users->Status->EditValue)) {
	$arwrk = $tb_users->Status->EditValue;
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($tb_users->Status->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " selected" : "";	
?>
<option value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
</option>
<?php
			}
}
?>
</select>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">User Group</td>
    <td<?php echo $tb_users->UserGroup->CellAttributes() ?>><span id="cb_x_UserGroup">
<?php
$arwrk = $tb_users->UserGroup->EditValue;
if (is_array($arwrk)) {
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($tb_users->UserGroup->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " checked" : "";
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="radio" name="x_UserGroup" id="x_UserGroup" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $tb_users->UserGroup->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
  </tr>
  <tr class="ewTableRow">
    <td class="ewTableHeader">Location</td>
    <td<?php echo $tb_users->LocationId->CellAttributes() ?>><span id="cb_x_LocationId">
<select id="x_LocationId" name="x_LocationId"<?php echo $tb_users->LocationId->EditAttributes() ?>>
<!--option value="">Please Select</option-->
<?php
if (is_array($tb_users->LocationId->EditValue)) {
	$arwrk = $tb_users->LocationId->EditValue;
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($tb_users->LocationId->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " selected" : "";	
?>
<option value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
</option>
<?php
			}
}
?>
</select>
</span></td>
  </tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="    Add    ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Load default values
function LoadDefaultValues() {
	global $tb_users;
	$tb_users->UserGroup->CurrentValue = 0;
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $tb_users;
	$tb_users->UserName->setFormValue($objForm->GetValue("x_UserName"));
	$tb_users->UserPWD->setFormValue($objForm->GetValue("x_UserPWD"));
	$tb_users->Status->setFormValue($objForm->GetValue("x_Status"));
	$tb_users->UserGroup->setFormValue($objForm->GetValue("x_UserGroup"));
	$tb_users->LocationId->setFormValue($objForm->GetValue("x_LocationId"));
}

// Restore form values
function RestoreFormValues() {
	global $tb_users;
	$tb_users->UserName->CurrentValue = $tb_users->UserName->FormValue;
	$tb_users->UserPWD->CurrentValue = $tb_users->UserPWD->FormValue;
	$tb_users->Status->CurrentValue = $tb_users->Status->FormValue;
	$tb_users->UserGroup->CurrentValue = $tb_users->UserGroup->FormValue;
	$tb_users->LocationId->CurrentValue = $tb_users->LocationId->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $tb_users;
	$sFilter = $tb_users->SqlKeyFilter();
	if (!is_numeric($tb_users->ID->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@ID@", ew_AdjustSql($tb_users->ID->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$tb_users->Row_Selecting($sFilter);

	// Load sql based on filter
	$tb_users->CurrentFilter = $sFilter;
	$sSql = $tb_users->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$tb_users->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $tb_users;
	$tb_users->ID->setDbValue($rs->fields('ID'));
	$tb_users->UserName->setDbValue($rs->fields('UserName'));
	$tb_users->UserPWD->setDbValue($rs->fields('UserPWD'));
	$tb_users->Status->setDbValue($rs->fields('Status'));
	$tb_users->UserGroup->setDbValue($rs->fields('UserGroup'));
	$tb_users->LocationId->setDbValue($rs->fields('LocationId'));
	$tb_users->Location->setDbValue($rs->fields('Location'));
	$tb_users->CreateDate->setDbValue($rs->fields('CreateDate'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $tb_users;

	// Call Row Rendering event
	$tb_users->Row_Rendering();

	// Common render codes for all row types
	// UserName

	$tb_users->UserName->CellCssStyle = "";
	$tb_users->UserName->CellCssClass = "";

	// UserPWD
	$tb_users->UserPWD->CellCssStyle = "";
	$tb_users->UserPWD->CellCssClass = "";

	// Status
	$tb_users->Status->CellCssStyle = "";
	$tb_users->Status->CellCssClass = "";

	// UserGroup
	$tb_users->UserGroup->CellCssStyle = "";
	$tb_users->UserGroup->CellCssClass = "";

	// LocationId
	$tb_users->LocationId->CellCssStyle = "";
	$tb_users->LocationId->CellCssClass = "";
	if ($tb_users->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($tb_users->RowType == EW_ROWTYPE_ADD) { // Add row

		// UserName
		$tb_users->UserName->EditCustomAttributes = "";
		$tb_users->UserName->EditValue = ew_HtmlEncode($tb_users->UserName->CurrentValue);

		// UserPWD
		$tb_users->UserPWD->EditCustomAttributes = "";
		$tb_users->UserPWD->EditValue = ew_HtmlEncode($tb_users->UserPWD->CurrentValue);

		// Status
		$tb_users->Status->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("A", "Active");
		$arwrk[] = array("T", "Terminated");
		array_unshift($arwrk, array("", "Please Select"));
		$tb_users->Status->EditValue = $arwrk;

		// UserGroup
		$tb_users->UserGroup->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("0", "User");
		$arwrk[] = array("1", "Admin");
		$tb_users->UserGroup->EditValue = $arwrk;

		// LocationId
		$tb_users->LocationId->EditCustomAttributes = "";
		$sSqlWrk = "SELECT `ID`, `LocationName` FROM `tb_locations`";
		$rswrk = $conn->Execute($sSqlWrk);
		$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
		if ($rswrk) $rswrk->Close();
		array_unshift($arwrk, array("", "Please Select"));
		$tb_users->LocationId->EditValue = $arwrk;
	} elseif ($tb_users->RowType == EW_ROWTYPE_EDIT) { // Edit row
	} elseif ($tb_users->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$tb_users->Row_Rendered();
}
?>
<?php

// Add record
function AddRow() {
	global $conn, $Security, $tb_users;

	// Check for duplicate key
	$bCheckKey = TRUE;
	$sFilter = $tb_users->SqlKeyFilter();
	if (trim(strval($tb_users->ID->CurrentValue)) == "") {
		$bCheckKey = FALSE;
	} else {
		$sFilter = str_replace("@ID@", ew_AdjustSql($tb_users->ID->CurrentValue), $sFilter); // Replace key value
	}
	if (!is_numeric($tb_users->ID->CurrentValue)) {
		$bCheckKey = FALSE;
	}
	if ($bCheckKey) {
		$rsChk = $tb_users->LoadRs($sFilter);
		if ($rsChk && !$rsChk->EOF) {
			$_SESSION[EW_SESSION_MESSAGE] = "Duplicate value for primary key";
			$rsChk->Close();
			return FALSE;
		}
	}
	$rsnew = array();

	// Field UserName
	$tb_users->UserName->SetDbValueDef($tb_users->UserName->CurrentValue, "");
	$rsnew['UserName'] =& $tb_users->UserName->DbValue;

	// Field UserPWD
	$tb_users->UserPWD->SetDbValueDef($tb_users->UserPWD->CurrentValue, "");
	$rsnew['UserPWD'] =& $tb_users->UserPWD->DbValue;

	// Field Status
	$tb_users->Status->SetDbValueDef($tb_users->Status->CurrentValue, "");
	$rsnew['Status'] =& $tb_users->Status->DbValue;

	// Field UserGroup
	$tb_users->UserGroup->SetDbValueDef($tb_users->UserGroup->CurrentValue, NULL);
	$rsnew['UserGroup'] =& $tb_users->UserGroup->DbValue;

	// Field LocationId
	$tb_users->LocationId->SetDbValueDef($tb_users->LocationId->CurrentValue, NULL);
	$rsnew['LocationId'] =& $tb_users->LocationId->DbValue;

	// Call Row Inserting event
	$bInsertRow = $tb_users->Row_Inserting($rsnew);
	if ($bInsertRow) {
		$conn->raiseErrorFn = 'ew_ErrorFn';
		$AddRow = $conn->Execute($tb_users->InsertSQL($rsnew));
		$conn->raiseErrorFn = '';
	} else {
		if ($tb_users->CancelMessage <> "") {
			$_SESSION[EW_SESSION_MESSAGE] = $tb_users->CancelMessage;
			$tb_users->CancelMessage = "";
		} else {
			$_SESSION[EW_SESSION_MESSAGE] = "Insert cancelled";
		}
		$AddRow = FALSE;
	}
	if ($AddRow) {
		$tb_users->ID->setDbValue($conn->Insert_ID());
		$rsnew['ID'] =& $tb_users->ID->DbValue;

		// Call Row Inserted event
		$tb_users->Row_Inserted($rsnew);
	}
	return $AddRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
